package com.sc.nft.admin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.nft.annotation.SysMemberLogin;
import com.sc.nft.dto.BusinessCardOrderDTO;
import com.sc.nft.entity.dto.BusinessCardSetDTO;
import com.sc.nft.entity.vo.BusinessCardOrderAdminVO;
import com.sc.nft.entity.vo.BusinessCardSetVO;
import com.sc.nft.enums.RedisKeyEnum;
import com.sc.nft.service.BusinessCardConfigService;
import com.sc.nft.service.BusinessCardOrderService;
import com.sc.nft.sup.ErrorCode;
import com.sc.nft.sup.PageResult;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.concurrent.TimeUnit;

/**
 * @auth zhangfofa
 * @date 2024/7/10 15:13:30
 */

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/admin/businessCard")
@Api(tags = "名片设置")
public class BusinessCardAdminController {

    private final Redisson redisson;
    private final BusinessCardOrderService businessCardOrderService;
    private final BusinessCardConfigService businessCardConfigService;
    /**
     * 名片设置
     */
    @PostMapping(value = "/set", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "名片设置"  , notes = "名片设置", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result businessCardSet(@Valid @RequestBody BusinessCardSetDTO dto) {
        RLock lock = redisson.getLock(RedisKeyEnum.BUSINESS_CARD_SET.getKey());
        try {
            boolean res = lock.tryLock(3, 10, TimeUnit.SECONDS);
            if (res) {
                businessCardConfigService.businessCardSet(dto);
            }else {
                return Result.fail(ErrorCode.LATER_TRY);
            }
        } catch (InterruptedException e) {
            return Result.fail("当前人数过多，请稍后再试");
        } finally {
            lock.unlock();
        }
        return Result.success();
    }

    /**
     * 名片设置
     */
    @PostMapping(value = "/get")
    @ApiOperation(value = "名片显示"  , notes = "名片显示", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<BusinessCardSetVO> businessCardGet() {
        BusinessCardSetVO businessCardSetVO = businessCardConfigService.businessCardGet();
        return Result.success(businessCardSetVO);
    }

    /**
     * 名片订单
     */
    @PostMapping(value = "/orderPage", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "名片订单"  , notes = "名片订单", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<BusinessCardOrderAdminVO> orderPage(@Valid @RequestBody BusinessCardOrderDTO request) {
        Page page = businessCardOrderService.adminBusinessCardOrderPage(request.toMp(),
                                                                        request.getOrderNo(),
                                                                        request.getUserTel(),
                                                                        request.getOrderStartTime(),
                                                                        request.getOrderEndTime(),
                                                                        request.getPayStartTime(),
                                                                        request.getPayEndTime());
        return PageResult.success(page);
    }
}